from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from datetime import date
from upload.models import UploadedImage
from django.http import HttpResponseRedirect
from settings import MEDIA_URL
from registration.models import UserProfile

from registration.backends import get_backend

# Create your views here.
    
def display(request):
    context = RequestContext(request)
    mv_comic = UploadedImage.objects.filter(times_visited = 0)
    mvcomics = []
    mvpaths = []
    for comic in mv_comic:
        m = comic.imagepath.name
        mvimage = MEDIA_URL + '/comics/' + m
        mvcomics.append(mvimage)
        mvpaths.append(m)
        
    nw_comic = UploadedImage.objects.filter(owner = request.user)#date = date.today())
    nwcomics = []
    nwpaths = []
    for comic in nw_comic:
        m = comic.imagepath.name
        nwimage = MEDIA_URL + '/comics/' + m
        nwcomics.append(nwimage)
        nwpaths.append(m)
    
    return render_to_response('viewer.html',
                              {'most_viewed': mvcomics,'mvpaths': mvpaths,
                               'newest': nwcomics,'nwpaths':nwpaths},
                                context_instance=context)

def view_profile(request):
    userProfiles = UserProfile.objects.filter(company = 'fail')
    for userP in userProfiles:
        if userP.logo:
            logo = MEDIA_URL +'/profile/'+ userP.logo.name
        else:
            logo = MEDIA_URL +'/profile/defaultUser.jpg'
        userProfile = userP
    context = RequestContext(request)
    return render_to_response('view_profile.html',
                              {'username':userProfile.username, 'logo':logo}
                              ,context_instance=context)

"""    
def view_newest(context):
    #today = datetime.date #PREGUNTAR
    nw_comic = UploadedImage.objects.filter(date = date(2010, 11, 27))
    comics = []
    for comic in nw_comic:
        m = comic.imagepath.name
        image = MEDIA_URL + '/comics/' + m
        comics.append(image)
    return render_to_response('viewer.html',
                                {'newest': comics},
                                context_instance=context)

  
def view_all_browsed(request):
    all_comic = UploadedImage.objects
    context = RequestContext(request)
    return render_to_response('viewer.html',{'most_viewed':mv_comic['imagepath']},
                                context_instance=context)
        
def sayThanks(request):
    context = RequestContext(request)
    return render_to_response('image_uploaded.html',context_instance=context)
"""